package com.vf.editor.mvc.scene.layer;

import java.util.LinkedList;
import java.util.List;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.vf.editor.common.entity.ZtreeNode;
import com.vf.editor.common.model.resources.Model;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.model.resources.SceneModel;
import com.vf.editor.common.model.resources.ScenePoint;
import com.vf.editor.common.model.resources.ScenePolyline;
import com.vf.editor.common.model.resources.SceneProperty;
import com.vf.editor.common.model.resources.SceneRoadMark;
import com.vf.editor.common.model.resources.SceneVmodel;
import com.vf.editor.common.model.resources.SceneWall;
import com.vf.editor.common.model.resources.ServerMap;
import com.vf.editor.common.render.Cartesian3;
import com.vf.editor.common.render.Orientation;
import com.vf.editor.common.util.FileUtil;


public class SceneLayerService {

	public List<ZtreeNode> getTree(String id,String show) {
		List<SceneLayer> list = getChildrenAllTree(id);
		List<ZtreeNode> nodelist = SceneLayer.toZTreeNode(list,show);
		return nodelist;
	}
	
	public List<SceneLayer> getChildrenAllTree(String pId) {
		List<SceneLayer> list = getChildrenByPid(pId);
		for (SceneLayer o : list) {
			o.setChildren(getChildrenAllTree(o.getId()));
		}
		return list;
	}

	public List<SceneLayer> getChildrenByPid(String id) {
		String sql = "SELECT a.*,(CASE WHEN (select count(id) from " + SceneLayer.TABLE_NAME
				+ " where a.id=parentId ) >0 THEN true ELSE false END) AS isParent FROM " + SceneLayer.TABLE_NAME
				+ " a where a.parentId=?  ORDER BY a.NAME ";
		return SceneLayer.dao.find(sql, id);
	}
	
	public void delete(SceneLayer layer) {
		if (StrKit.equals("1", layer.getType()) || StrKit.equals("2", layer.getType())) {
			SceneModel.dao.deleteById(layer.getId());
		}
		else if(StrKit.equals("3", layer.getType())){
			if(SceneVmodel.dao.deleteById(layer.getId())) {
				FileUtil.deleteFolder(PathKit.getWebRootPath()+"/upload/vmodel/"+layer.getId());
			}
		}else {
		}
	}
	
	public void deletes(SceneLayer layer) {
		if (StrKit.equals("-1", layer.getType())){
			List<SceneLayer> layers = SceneLayer.dao.find("select * from "+SceneLayer.TABLE_NAME+" where parentId=? ",layer.getId());
			for (SceneLayer item : layers) {
				if(item.delete()) {
					delete(item);
				}
			}
		}else {
			delete(layer);
		}
	}

	public List<SceneLayer> getThings(String id) {
		List<SceneLayer> objects=new LinkedList<SceneLayer>();
		List<SceneLayer> layers = SceneLayer.dao.find("select * from "+SceneLayer.TABLE_NAME+" where parentId=? ",id);
		SceneLayer thing;
		for(SceneLayer layer:layers) {
			thing= getThing( layer);
			if(thing!=null) {
				objects.add(thing);
			}
		}
		return objects;
	}
	
	/**
	 * 组装实体
	 * @param layer
	 * @return
	 */
	public SceneLayer getThing(SceneLayer layer) {
		if(StrKit.equals("0",layer.getType())) {
			ServerMap serverMap=ServerMap.dao.findById(layer.getThingId());
			if(serverMap!=null) {
				serverMap.setId(layer.getId());
				layer.setThing(serverMap);
				return layer;
			}
		}
		else if(StrKit.equals("1",layer.getType()) || StrKit.equals("2",layer.getType())) {
			SceneModel sceneModel=SceneModel.dao.findById(layer.getId());
			if(sceneModel!=null) {
				Model model=Model.dao.findById(layer.getThingId());
				if(model!=null) {
					sceneModel.setUrl(model.getUrl());
					System.err.print(model.getIsRelative());
					sceneModel.setIsRelative(model.getIsRelative());
					layer.setThing(sceneModel);
					
					Cartesian3 position =new Cartesian3();
					position.setX(sceneModel.getX());
					position.setY(sceneModel.getY());
					position.setZ(sceneModel.getZ());
					layer.setPosition(position);
					Orientation orientation =new Orientation();
					orientation.setHeading(sceneModel.getHeading());
					orientation.setPitch(sceneModel.getPitch());
					orientation.setRoll(sceneModel.getRoll());
					layer.setOrientation(orientation);
					return layer;
				}
			}
		}
		//视频模型
		else if(StrKit.equals("3",layer.getType())) {
			SceneVmodel sceneVmodel=SceneVmodel.dao.findById(layer.getId());
			if(sceneVmodel!=null) {
				Cartesian3 position =new Cartesian3();
				position.setX(sceneVmodel.getX());
				position.setY(sceneVmodel.getY());
				position.setZ(sceneVmodel.getZ());
				layer.setPosition(position);
				Orientation orientation =new Orientation();
				orientation.setHeading(sceneVmodel.getHeading());
				orientation.setPitch(sceneVmodel.getPitch());
				orientation.setRoll(sceneVmodel.getRoll());
				layer.setOrientation(orientation);
				layer.setThing(sceneVmodel);
				return layer;
			}
		}
		else if(StrKit.equals("ERoadMark",layer.getType())) {
			SceneRoadMark sceneRoadMark=SceneRoadMark.dao.findById(layer.getId());
			if(sceneRoadMark!=null) {
				layer.setThing(sceneRoadMark);
				return layer;
			}
		}
		else if(StrKit.equals("Point",layer.getType())) {
			ScenePoint scenePoint=ScenePoint.dao.findById(layer.getId());
			if(scenePoint!=null) {
				Cartesian3 position =new Cartesian3();
				position.setX(scenePoint.getX());
				position.setY(scenePoint.getY());
				position.setZ(scenePoint.getZ());
				layer.setPosition(position);
				List<SceneProperty> propertys = SceneProperty.dao.find("select * from "+SceneProperty.TABLE_NAME+" where thingId=? ",layer.getId());
				layer.setPropertys(propertys);
				layer.setThing(scenePoint);
				return layer;
			}
		}
		else if(StrKit.equals("Polyline",layer.getType())) {
			ScenePolyline scenePolyline=ScenePolyline.dao.findById(layer.getId());
			if(scenePolyline!=null) {
				List<SceneProperty> propertys = SceneProperty.dao.find("select * from "+SceneProperty.TABLE_NAME+" where thingId=? ",layer.getId());
				layer.setPropertys(propertys);
				layer.setThing(scenePolyline);
				return layer;
			}
		}else if(StrKit.equals("Wall",layer.getType())) {
			SceneWall sceneWall = SceneWall.dao.findById(layer.getId());
			List<SceneProperty> propertys = SceneProperty.dao.find("select * from "+SceneProperty.TABLE_NAME+" where thingId=? ",layer.getId());
			layer.setPropertys(propertys);
			layer.setThing(sceneWall);
			return layer;
		}
		return null;
	}
	
}
